package com.chixing.ih.module.task.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.chixing.ih.config.AlipayConfig;
import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.task.pojo.Order;
import com.chixing.ih.module.task.service.IOrderService;
import com.chixing.ih.module.task.service.IPayService;
import com.chixing.ih.status.PaymentEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 目的:
 *
 * @author ：zcw
 * @version: 1.0  2020/10/13 19:49
 */
@RestController
@RequestMapping("pay")
public class PayController {

    @Autowired
    private IPayService IPayService;

    @Autowired
    private AlipayConfig alipayConfig;

    @Autowired
    private IOrderService iOrderService;


    @ApiOperation(value = "支付接口", notes = "支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNum", value = "订单编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "salary", value = "金额", required = true, dataType = "Double"),
    })
    @ApiResponses({
            @ApiResponse(code = 204, message = "操作成功"),
            @ApiResponse(code = 400, message = "操作失败"),
            @ApiResponse(code = 500, message = "程序内部代码出现问题")
    })
    @GetMapping("pay/{orderId}")
    public void toPay(@PathVariable("orderId") Long orderId, HttpServletResponse response,
                      HttpServletRequest request) throws IOException,
            AlipayApiException {
        IPayService.pay(orderId, response, request);

    }



    @GetMapping("notifyUrl")
    public void notifyUrl(HttpServletRequest request) throws Exception {
        Order order = new Order();
        // 订单号
        Long orderId = (Long) request.getAttribute("orderId");
        order.setOrderId(orderId);
            order.setPaymentStatus(PaymentEnum.ONE.status);
            iOrderService.updateOrder(order);
    }

    @GetMapping("returnUrl")
    public Map<String, Object> alipayReturn(HttpServletRequest request) throws AlipayApiException {

        return ResponseData.REP_ACCEPTED_SUCCESS();
    }
}
